<template>
  <layout-body>
    <div>
      <el-button
        type="primary"
        v-permission="'store-goods_cate-create'"
        @click="createClick(0)"
      >
        <el-icon><Plus /></el-icon>
        添加商品分类
      </el-button>
    </div>
    <!-- 列表 -->
    <el-table
      v-loading="initLoading"
      class="mt20"
      :data="lists"
      row-key="id"
      default-expand-all
      type="expand"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="分类名称" prop="cate_name">
        <template #default="scope">
          <span>{{ scope.row.html }}</span>
          <span>{{ scope.row.cate_name }}</span>
        </template></el-table-column
      >
      <el-table-column label="分类图标" prop="icon">
        <template #default="scope">
          <el-image
            style="width: 40px; height: 40px"
            :preview-src-list="[scope.row.icon]"
            preview-teleported
            :src="scope.row.icon"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column label="分类大图" prop="icon">
        <template #default="scope">
          <el-image
            style="height: 40px"
            :z-index="9999"
            :preview-src-list="[scope.row.image]"
            preview-teleported
            :src="scope.row.image"
            fit="fill"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="排序"
        prop="sort"
        align="center"
      ></el-table-column>
      <el-table-column label="状态" prop="status">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status.value"
            size="small"
            :active-value="1"
            :inactive-value="0"
            :loading="scope.row.loading"
            :before-change="
              () => {
                return (scope.row.switch = true);
              }
            "
            @change="statusChange($event, scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="create_time">
        <template #default="scope">
          <span class="text-grey fz12">{{ scope.row.create_time }}</span>
        </template></el-table-column
      >
      <el-table-column align="left" label="操作" width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="createClick(scope.row.id)"
            v-permission="'store-goods_cate-update'"
            >编辑</el-button
          >

          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="不用了"
            icon="WarningFilled"
            icon-color="red"
            title="确定删除吗？"
            @confirm="del(scope.row.id)"
          >
            <template #reference>
              <el-button
                link
                type="primary"
                v-permission="'store-goods_cate-delete'"
                >删除</el-button
              >
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </layout-body>
  <!-- 添加 -->
  <storeGoodsCateCreate
    ref="storeGoodsCateCreateRef"
    @success="toInit()"
  ></storeGoodsCateCreate>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance, onMounted } from "vue";
import storeGoodsCateCreate from "./create.vue";

import {
  getGoodsCateList,
  updateStatusGoodsCate,
  delGoodsCate,
} from "@/api/store/goods_cate";
import { Result, ResultError } from "@/types";

const {
  proxy,
  proxy: { $utils },
} = getCurrentInstance() as any;

const storeGoodsCateCreateRef = ref<HTMLElement>();

const createClick = (id: number) => {
  proxy?.$refs["storeGoodsCateCreateRef"]?.open(id);
};

const initLoading = ref<boolean>(false);

const lists = ref<any>();

const toInit = () => {
  initLoading.value = true;
  getGoodsCateList({})
    .then((res: Result) => {
      lists.value = res.data;
      setTimeout(() => {
        initLoading.value = false;
      }, 300);
    })
    .catch((err: ResultError) => {
      $utils.errorMsg(err);
    });
};
const statusChange = (
  val: number,
  row: {
    switch: boolean;
    loading: boolean;
    id: number;
  }
) => {
  if (row.switch === true) {
    row.loading = true;
    updateStatusGoodsCate({
      id: row.id,
      status: val,
    })
      .then((res: Result) => {
        row.loading = false;
        toInit();
        $utils.successMsg(res);
      })
      .catch((err: ResultError) => {
        row.loading = false;
        toInit();
        $utils.errorMsg(err);
      });
  }
};

const del = (id: number) => {
  delGoodsCate({
    id,
  })
    .then((res: Result) => {
      toInit();
      $utils.successMsg(res.message);
    })
    .catch((err: ResultError) => {
      $utils.errorMsg(err);
    });
};
onMounted(() => {
  toInit();
});
</script>